import { _banner } from '../../common/api/banner'
import { _list } from '../../common/api/projects'
const computedBehavior = require('miniprogram-computed')
const app = getApp()
Component({
  behaviors: [computedBehavior],
  data: {
    logged: false,
    userinfo: null,
    sliders: [],
    current: 0,
    lists: []
  },
  computed: {
    tag() {
      let tagStr = this.data.userinfo
          ? this.data.userinfo.AgentInfo.SalerType
          ? this.data.userinfo.AgentInfo.SalerType
          : this.data.userinfo.AgentInfo.AgentType
        : '赚钱达人'
      return tagStr
    },
    addBar () {
      return /iphone\sx/i.test(app.store.phone.toLowerCase())
    }
  },
  methods: {
    swiperChange(e) {
      this.setData({
        current: e.detail.current
      })
    },
    recommand(e) {
      console.log(this.data.logged)
      console.log(e.currentTarget.dataset.id)
      // e.preventDefault()
    },
    recommandGuest() {
      if (!this.data.userinfo) {
        wx.showModal({
          title: '对不起',
          content: '您还未注册，请先注册',
          success: r => {
            if (r.confirm) {
              wx.navigateTo({
                url: '/pages/signin/choose'
              })
            }
          }
        })
      } else {
        // 去推荐客户
        console.log('去推荐客户')
        wx.navigateTo({
          url: '/pages/recommand/index'
        })
      }
    },
    totalQuery() {
      app.loading('加载中')
      Promise.all([
        _banner(),
        _list()
      ]).then(res => {
        wx.hideLoading()
        wx.stopPullDownRefresh()
        let sliders = res[0].data.Data
        let lists = res[1].data.Data.map(item => {
          item.Instruction = item.Instruction.replace(/、/g, '|')
          item.Tag = item.Tag.split('、')
          item.coordinates = {
            latitude: item.PointY,
            longitude: item.PointX
          }
          return item
        })
        this.setData({
          sliders,
          lists
        })
      }).catch(err => {
        wx.hideLoading()
        wx.stopPullDownRefresh()
        console.log(err)
        wx.showModal({
          title: '提示',
          content: JSON.stringify(err),
        })
      })
    },
    onLoad() {
      // 查询列表、banner
      this.totalQuery()
    },
    onShow () {
      // 获取身份
      app.updateUserInfo({
        force: true
      }).then(res => {
        console.log(res)
        this.setData({
          userinfo: res
        })
      }).catch(err => {
        console.log(err)
      })
    },
    onPullDownRefresh(){
      this.totalQuery()
    },
    onShareAppMessage () {
      return app.shareInfo
    }
  }
})
